package org.entities;

import org.BlockMap;
import org.GameState;
import org.Progress;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Sound;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.SlickException;

public class Elevator extends LevelObject {

    private float dy=0;
    public static final float MOVE_SPEED=0.1f;    // velocity for elevator
    public Animation fallback;                     // animation to render
    private static int size[]={-10,14,26,14,26,20,-10,20};
    public static boolean moveUp;
    public static boolean moveDown;
    public static boolean elevExist;
    private Sound elevator;
    private boolean playOnce;
    
    public Elevator(int x, int y, String type) throws SlickException {

        super(x, y,
                x+size[0],y+size[1], 
                x+size[2],y+size[3],
                x+size[4],y+size[5],
                x+size[6],y+size[7],
                type);


        SpriteSheet sheet = new SpriteSheet("data/anim/elevator.png",48,16);
        fallback = new Animation();
        for (int frame=0;frame<3;frame++) {
            fallback.addFrame(sheet.getSprite(frame,0), 250);
        }        
        fallback.setAutoUpdate(false);
        elevExist=true;
        System.out.printf("createElevator=[%s]\n",type);        
        elevator = new Sound("data/sound/elevator2.ogg");
    }

    public void update(int delta) throws SlickException {

        dy = (float)delta*MOVE_SPEED ;
        float ay = dy;        
        y= poly.getY();
        x= poly.getX();       

        // ========================
        // elevator movements code
        // ========================


        if (moveDown){
            if (!playOnce){
            elevator.play(1,Progress.sfxVol/20);
            playOnce=true;
            }
            fallback.update(delta);
            y+=ay;
            poly.setY( y);
            BlockMap.Samus.x= x-6;
            BlockMap.Samus.poly.setX( x);
            BlockMap.Samus.y= y-48;
            BlockMap.Samus.poly.setY( y-50);
            Player.Freeze=true;
        }

        if (GameState.entityCollisionWith(this)){
            Player.Freeze=false;
            moveDown=false;
            die();
            elevExist=false;
        }


        if ( moveUp){
            if (!playOnce){
            elevator.play(1,Progress.sfxVol/20);
            playOnce=true;
            }
            fallback.update(delta);
            y-=ay;
            poly.setY( y);
            BlockMap.Samus.x= x-6;
            BlockMap.Samus.poly.setX( x);
            BlockMap.Samus.y= y-48;
            BlockMap.Samus.poly.setY( y-50);
            Player.Freeze=true;
        }
        if (GameState.entityCollisionWith(this)){
            Player.Freeze=false;
            moveUp=false;
            die();
            elevExist=false;
        }

        if (x < 0)
            x = 0;
        if (y < 0)
            y = 0;
        if (x > BlockMap.mapWidth -48)
            x = BlockMap.mapWidth -48;
        if (y > BlockMap.mapHeight-48)
            y = BlockMap.mapHeight-48;
    }

    public void die() {
        System.out.printf("elev must die\n");
        elevator.stop();
        playOnce=false;
        BlockMap.entities.remove(this);
    }

    public boolean isCollisionWith(LevelObject other) {

        if (other.type.equals("elevKill")&&moveDown){
            if (poly.intersects(other.poly)){
            System.out.println("elev intersected with elevkill");
            return poly.intersects(other.poly);
            }
        }
        if (other.type.equals("elevStop")&&moveUp){ 
            if (poly.intersects(other.poly)){
            System.out.println("elev intersected with elevstop");
            return poly.intersects(other.poly);
            }
        }
        return false;        
    }

    public boolean collidedWith(LevelObject entity) {
        return true;
    }


    public void draw(Graphics g) {
        g.drawAnimation(fallback, x-6, y);
        if (debugMode)
            g.draw(poly); 
    }
}